
/**
 * 系统设置
 */

import { DependencyContainer, ROOT, Request } from '@sker/core'
import { join } from 'path'
import { z } from 'zod'
import { writeFileSync } from 'fs-extra'
export default (injector: DependencyContainer) => {
    const typeSchema = z.object({
        type: z.enum(['database', 'redis', 'rabbitmq', 'email', 'site', 'mongose']),
    })
    const databaseSchema = z.object({
        url: z.string(),
        type: z.string()
    })
    const req = injector.resolve(Request)
    const type = typeSchema.parse(req.body)
    const root = injector.resolve(ROOT)

    switch (type.type) {
        case 'database':
            const database = databaseSchema.parse(req.body.database)
            const configPath = join(`${root}`, 'data', 'database.json')
            writeFileSync(configPath, JSON.stringify(database, null, 2))
            break;
        case 'email':
        case 'redis':
        case 'rabbitmq':
        default:
            return { reason: '未知类型' }
    }
}